package com.byapp.fast.amqp.receiver;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.byapp.fast.common.enums.ItemStatusEnum;
import com.byapp.fast.common.utils.ResultUtils;
import com.byapp.fast.common.vo.ResultVO;
import com.byapp.fast.modules.contract.dto.CallBackEthDTO;
import com.byapp.fast.modules.contract.dto.CallBackPayDTO;
import com.byapp.fast.modules.contract.dto.CancelOrderDTO;
import com.byapp.fast.modules.contract.service.ContractService;
import com.byapp.fast.modules.match.entity.MatchOddsEntity;
import com.byapp.fast.modules.match.service.MatchService;
import com.byapp.fast.modules.order.entity.OrderItemEntity;
import com.byapp.fast.modules.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

@Component
@Slf4j
public class CallBackEventReceiver {

    @Autowired
    private ContractService contractService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private MatchService matchService;

    @RabbitListener(queues = "#{callBackPayQueue.name}")
    public ResultVO callBackPayReceive(CallBackPayDTO callBackPayDTO, Channel channel) throws IOException {
        log.info("【支付回调】消息处理开始");
        contractService.callBackPay(callBackPayDTO);
        log.info("【支付回调】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{callBackEthQueue.name}")
    public ResultVO callBackEthReceive(CallBackEthDTO callBackEthDTO) {
        log.info("【区块回调】消息处理开始");
        String txHash = callBackEthDTO.getTxHash();
        String betId = callBackEthDTO.getCallBackBetDTO().getBetId();
        String oddsId = callBackEthDTO.getCallBackBetDTO().getPlayId();

        MatchOddsEntity matchOddsEntity = matchService.queryMatchOdds(oddsId);
        if (matchOddsEntity == null) {
            log.error("【区块回调】matchOddsEntity不存在，oddsId={}", oddsId);
            return ResultUtils.error();
        }

        // 更新订单状态
        Integer itemStatus = contractService.callBackEth(callBackEthDTO);
        log.info("【更新订单状态】itemStatus={}", itemStatus);
        OrderItemEntity orderItemEntity = orderItemService.selectOne(new EntityWrapper<OrderItemEntity>().eq("TXHASH", txHash));
        if (orderItemEntity == null) {
            log.error("【区块回调】orderItemEntity不存在，txHash={}", txHash);
            return ResultUtils.error();
        }
        orderItemEntity.setItemStatus(itemStatus);
        orderItemService.updateById(orderItemEntity);

        // 若订单失败则进行退款
        if (Objects.equals(itemStatus, ItemStatusEnum.ITEM_ERROR_ING.getCode())) {
            log.info("【数据校验失败】itemStatus={}", itemStatus);
            CancelOrderDTO cancelOrderDTO = new CancelOrderDTO();
            cancelOrderDTO.setBetid(betId);
            cancelOrderDTO.setDesc("数据校验失败");
            contractService.callCancelOrder(cancelOrderDTO);
            orderItemEntity.setItemStatus(ItemStatusEnum.ITEM_ERROR.getCode());
            orderItemService.updateById(orderItemEntity);
        }
        log.info("【区块回调】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{cbAddMatchQueue.name}")
    public ResultVO cbAddMatchReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-添加赛事】消息处理开始");
        log.info("【区块回调-添加赛事】消息处理结束");
        return ResultUtils.success();
    }


    @RabbitListener(queues = "#{cbAddPlayQueue.name}")
    public ResultVO cbAddPlayReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-添加玩法】消息处理开始");
        log.info("【区块回调-添加玩法】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{cbChangeOddsQueue.name}")
    public ResultVO cbChangeOddsReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-变更赔率】消息处理开始");
        log.info("【区块回调-变更赔率】消息处理结束");
        return ResultUtils.success();
    }


    @RabbitListener(queues = "#{cbChangeHandicapQueue.name}")
    public ResultVO cbChangeHandicapReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-变更封盘】消息处理开始");
        log.info("【区块回调-变更封盘】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{cbChangeMatchQueue.name}")
    public ResultVO cbChangeMatchReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-赛事改期】消息处理开始");
        log.info("【区块回调-赛事改期】消息处理结束");
        return ResultUtils.success();
    }


    @RabbitListener(queues = "#{cbAddBailQueue.name}")
    public ResultVO cbAddBailReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-增加保证金】消息处理开始");
        log.info("【区块回调-增加保证金】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{cbCancelOrderQueue.name}")
    public ResultVO cbCancelOrderReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-取消订单】消息处理开始");
        log.info("【区块回调-取消订单】消息处理结束");
        return ResultUtils.success();
    }


    @RabbitListener(queues = "#{cbCancelMatchQueue.name}")
    public ResultVO cbCancelMatchReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-取消赛事】消息处理开始");
        log.info("【区块回调-取消赛事】消息处理结束");
        return ResultUtils.success();
    }

    @RabbitListener(queues = "#{cbLotMatchQueue.name}")
    public ResultVO cbLotMatchReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-赛事开奖】消息处理开始");
        log.info("【区块回调-赛事开奖】消息处理结束");
        return ResultUtils.success();
    }


    @RabbitListener(queues = "#{cbPrizeMatchQueue.name}")
    public ResultVO cbPrizeMatchReceiver(CallBackEthDTO callBackEthDTO) throws IOException {
        log.info("【区块回调-赛事派奖】消息处理开始");
        log.info("【区块回调-赛事派奖】消息处理结束");
        return ResultUtils.success();
    }


}
